<?php

//include ("lib//lua_converter.class.php");

class doNothingTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      //new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      //new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      new sfCommandOption('file', null, sfCommandOption::PARAMETER_REQUIRED, 'LUA File to convert', 'data/RPBot.lua'),
      // add your own options here
    ));

    $this->namespace        = 'raidpoints';
    $this->name             = 'doNothing';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [import|INFO] task does things.
Call it with:

  [php symfony import|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
    // add your code here
    //
    $this->logsection("import", "Opening Saved Variables file...");

    $lua = new LuaConverter();
    //copy($options["file"], "web/upload/".date("m.d.y H-i-s").".lua");
    $raiddata = ($lua->convert(utf8_decode(file_get_contents($options["file"]))));

    $this->logsection("import", "Loading Saved Variables...");

    eval($raiddata);

    if (isset($rpDEBUGBotDB))
	{
		$arrayi = $rpDEBUGBotDB;
	}
	else
	{
		$arrayi = $rpBotDB;
	}

    $sortedraid = $arrayi["realm"];

    $this->logsection("import", "Itterating Saved Variables...");
	foreach ( $sortedraid as $realm => $rdata )
	{
		foreach ( $rdata["player"] as $player => $pdata )
		{
            $query = Doctrine::getTable("Player")
                ->createQuery()
                ->where("name=?", strtolower(utf8_encode($pdata["name"])));
            $result = $query->execute();
            if ($result->count() == 0)
            {
                $p = new Player();
                $p->setFullname(utf8_encode($pdata["fullname"]));
                $p->setName(utf8_encode($pdata["name"]));
                $p->setAltid(1);
                $p->setGuildedflag(1);

                $query = Doctrine::getTable("PClass")
                    ->createQuery()
                    ->where("LOWER(class)=?", strtolower($pdata["class"]));
                $result = $query->execute();
                $c = $result->getFirst();
                $p->setPClass($c);
                //print_r($p->toArray());
                try
                {
                    $p->trySave();
                    $this->logsection("import", "New Player: ".$p->getFullname());
                    $rdata["player"][$player]["id"] = $p->getId();
                }
                catch (exception $ex)
                {

                }
            }
        }
        foreach ( $rdata["raid"] as $raid => $value )
        {
            $query = Doctrine::getTable("Raid")
                ->createQuery()
                ->where("short=?", $raid)
                ->andWhere("server=?", $realm);
            $result = $query->execute();
            $r = null;
            if ($result->count() > 0)
                $r = $result->getFirst();
            else
            {
                $r = new Raid();
                $r->setRaid($raid);
                $r->setShort($raid);
                $r->setShown(0);
                $r->setServer($realm);
            }

            $query = Doctrine::getTable("Officer")
                ->createQuery()
                ->where("id=?", 0);
            $result = $query->execute();
            $officer = $result->getFirst();

            $query = Doctrine::getTAble("TType")
                ->createQuery()
                ->where("ttype=?", "P");
            $type_p = $result->getFirst();

            $actiont = 0;
            ksort($value);
            foreach ($value as $player => $data)
            {
                ksort($data["recentactions"]);
                foreach ( $data["recentactions"] as $actiontime => $ev)
                {
                    $history = new History();
                    $history->fromArray($ev);

                    if ($actiontime > $actiont)
                        $actiont = $actiontime;

                    $waitlist = 0;
                    if ($ev["waitlist"] == 1)
                        $waitlist = 1;
                    $history->setWaitlistflag($waitlist);
                    $history->setZoneid(0);

                    //echo "- dates - ";
                    $history->setSavedate(($actiontime));
                    $history->setActiondate(($ev["actiontime"]));
                    $history->setDate(($ev["datetime"]));
                    $history->setOfficer($officer);
                    $history->setRaid($r);

                    //echo "ttype - ";
                    //print_r($ev);
                    $query = Doctrine::getTable("TType")
                        ->createQuery()
                        ->where("ttype=?", $ev["ty"]);
                    $result = $query->execute();
                    //echo $result->count();
                    if ($result->count() > 0)
                        $history->setTType($result->getFirst());
                    else
                         $history->setTtype($type_p);

                    //echo "aaction - ";
                    $query = Doctrine::getTable("Aaction")
                        ->createQuery()
                        ->where("name=?", $ev["action"]);
                    $result = $query->execute();
                    if ($result->count() > 0)
                        $history->setAaction($result->getFirst());
                    else
                        $history->setAaction("I");

                    //echo "reason - ";
                    $query = Doctrine::getTable("Reason")
                        ->createQuery()
                        ->where("reason=?", $ev["reason"]);
                    $result = $query->execute();
                    if ($result->count() > 0)
                        $history->setReason($result->getFirst());
                    else
                    {
                        $reason = new Reason();
                        $reason->setReason($ev["reason"]);
                        $reason->save();
                        $history->setReason($reason);
                    }

                    //echo "player - ";
                    if ($rdata["player"][$player]["id"] > 0)
                        $history->setPlayerid($rdata["player"][$player]["id"]);
                    else
                    {
                        $query = Doctrine::getTable("Player")
                                        ->createQuery()
                                        ->where("name=?", ($player));
                        $result = $query->execute();
                        if ($result->count() > 0)
                            $history->setPlayer($result->getFirst());
                    }

                    try
                    {
                        $history->trySave();
                        $this->logsection("import", "New Record: ".$history->getPlayer()->getFullname()." - ".$history->getDate()." ".$history->getValue());
                    }
                    catch (exception $ex)
                    {
                        $this->logsection("import", "Old Record: ".$history->getPlayer()->getFullname()." - ".$history->getDate()." ".$history->getValue());
                    }

                }

            }
            $this->logsection("import", "Update Raid: Version - ".$actiont);
            $r->setVersion($actiont);
            $r->save();
        }
    }

    $this->logsection("import", "Update Points");
    Doctrine::getTable("PlayerHistory")->updatePoints();
    $this->logsection("import", "Finished data import!");

    
    /*
    $r = Doctrine::getTable("History")->getLastLog('2009-07-05 17:01:59', 10, 1);
    $r = $r->getFirst();

    $r = Doctrine::getTable("PlayerHistory")->updatePoints(2, 1);

    $newHistory = new History();
    $newHistory->setDate('2009-07-05 17:01:59');
    $newHistory->setActiondate('2009-07-05 17:02:51');
    $newHistory->setSavedate($newHistory->getActiondate());
    $newHistory->setLogdate(time());
    $newHistory->setLogcomment(' ');
    $newHistory->setComment(' ');
    $newHistory->setItemid(0);
    $newHistory->setOfficerid(0);
    $newHistory->setPlayerid(10);
    $newHistory->setRaidid(1);
    $newHistory->setReasonid(831);
    $newHistory->setTtype('I');
    $newHistory->setValue('0');
    $newHistory->setWaitlistflag(0);
    $newHistory->setZoneid(0);
    $newHistory->setAaction("D");
    $newHistory->save();

    $this->logsection("do-nothing", "I did nothing...");
    //print_r($r->getAction());
    */
    

  }
}
